//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.IO;

using DNAide.Core;

namespace DNAide.Xml
{
	public partial class XmlAide
	{
	
		private ArrayList _validateExceptions;

		/// <summary>
		/// ArrayList of Validation Exceptions
		/// </summary>
		public ArrayList ValidateExceptions
		{
			get {
				if (_validateExceptions == null)
					_validateExceptions = new ArrayList();
				return _validateExceptions; 
			}
		}


		/// <summary></summary>
		public XmlAide()
		{
		}


		/// <summary>
		/// Validate an Xml document from a string
		/// </summary>
		/// <param name="value"></param>
		public Boolean ValidateXml(String value)
		{
			return ValidateXml(value, null, null);
		}

		/// <summary>
		/// Validate an Xml document from a string
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetNamespace"></param>
		/// <param name="schema"></param>
		public Boolean ValidateXml(String value, String targetNamespace, String schema)
		{
			ValidateExceptions.Clear();

			// Load the string into a StringReader
			StringReader sr = new StringReader(value);
			XmlReaderSettings settings = new XmlReaderSettings();

			if (!string.IsNullOrEmpty(schema))
			{
				settings.Schemas.Add(targetNamespace, schema);
				settings.ValidationType = ValidationType.Schema;
				settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
			}
			
			XmlReader reader = XmlReader.Create(sr, settings);

			try
			{
				while (reader.Read()) ;
			}
			catch (XmlException e)
			{
				ValidateExceptions.Add(e);
			}
			
			reader.Close();
			sr.Close();

			if (ValidateExceptions.Count > 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		/// <summary>
		/// Validate an Xml document from a uri
		/// </summary>
		/// <param name="value"></param>
		public Boolean ValidateXmlUri(String value)
		{
			return ValidateXmlUri(value, null, null);
		}

		/// <summary>
		/// Validate an Xml document from a uri
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetNamespace"></param>
		/// <param name="schema"></param>
		public Boolean ValidateXmlUri(String value, String targetNamespace, String schema)
		{
			ValidateExceptions.Clear();

			XmlReaderSettings settings = new XmlReaderSettings();

			if (!string.IsNullOrEmpty(schema))
			{
				settings.Schemas.Add(targetNamespace, schema);
				settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
				settings.ValidationType = ValidationType.Schema;
			}

			XmlReader reader = XmlReader.Create(value, settings);

			try
			{
				while (reader.Read()) ;
			}
			catch (XmlException e)
			{
				ValidateExceptions.Add(e);
			}

			reader.Close();

			if (ValidateExceptions.Count > 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}


		private void ValidationCallback(Object sender, ValidationEventArgs e)
		{
			ValidateExceptions.Add(e.Exception);
		}

	}
}
